localStorage.removeItem("token");
let reAccount = /^[0-9]{9,9}$/,
    rePassword =/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,16}$/;


$('.submit-button').on('click',() => {
    LoginAjax()
});

$(document).on('keyup',e => {
    if (e.keyCode === 13){
        LoginAjax()
    }
});

$('#password').on('blur',function () {
    JudgePassword();
}).on('focus',function () {
    $(this).parent().find('div').css('display','none');
});
$('#account').on('blur',function () {
    JudgeId();
}).on('focus',function () {
    $(this).parent().find('div').css('display','none');
});

//判断密码
function JudgePassword() {
    if (!rePassword.test($('#password').val())){
        passwordJudge(1);
        return false;
    }else {
        return true;
    }
}

//判断学号
function JudgeId() {
    if (!reAccount.test($('#account').val())){
        accountJudge(1);
        return false;
    }else {
        return true;
    }
}

function LoginAjax() {
    JudgeId();JudgePassword();

    if (JudgeId() && JudgePassword()){
        var login = Sendajax('http://119.23.46.241:8080/home/adminlogin','post',{
            id:$('#account').val(),
            password:$('#password').val()
        },'');

        if (login.message.indexOf('学号') !== -1 || login.message.indexOf('用户') !== -1){
            accountJudge(1);
        }else {
            accountJudge(0);
            login.message.indexOf('密码') !== -1?
                passwordJudge(1): passwordJudge(0);
        }

        if (login.code === 201 && login.data !== 0){
            passwordJudge(0);
            localStorage.setItem("token", login.data.token);
            window.location.href = 'uploadFinalGradePage.html';
        }else if (login.data === 0){
            accountJudge(1);
            passwordJudge(1);
        }
    }
}

function accountJudge(num) {
    let accountRight = $('.account-wrap div');
    accountRight.css('display','block');
    num === 0?accountRight.css({background:'url("https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Right%403x.png") no-repeat',backgroundSize:'cover'}):
        accountRight.css({background:'url("https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Delete%403x.png") no-repeat',backgroundSize:'cover'});
}

function passwordJudge(num) {
    let passwordRight = $('.password-wrap div');
    passwordRight.css('display','block');
    num === 0?passwordRight.css({background:'url("https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Right%403x.png") no-repeat',backgroundSize:'cover'}):
        passwordRight.css({background:'url("https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Delete%403x.png") no-repeat',backgroundSize:'cover'});
}